<?php

require_once 'common/common.inc.php';

$service_code = "KNIPPEN";

$schedule_date = isset($_REQUEST["schedule_date"]) ? $_REQUEST["schedule_date"] : null;
$location_code = isset($_REQUEST["location_code"]) ? $_REQUEST["location_code"] : null;
$start_date = new DateTimeDutch("@".strtotime($schedule_date), $timezone);
$start_date->setTimezone($timezone); 

$schedule_id = null;
$reserved = false;
$reserved_by_sign_in = false;
$reservation_id = null;
$reservation_date = null;
$previous_or_next_is_reserved = true;

if ($schedule_date != null) {

  $sql = "";
  $sql .= "SELECT ID, NAME, START, END, DATE_FORMAT(START,'%Y-%m-%d') START_SHORT_ISO ";
  $sql .= "FROM SCHEDULES ";
  $sql .= "WHERE DATE_FORMAT(START,'%Y-%m-%d') = '$schedule_date' ";
  $sql .= "AND LOCATION_CODE = '$location_code' ";
  $sql .= "AND SERVICE_CODE = '$service_code' ";
  //echo $sql;
  
  conn();
  $result = mysql_query($sql);
  if (!$result) {die('Could not exec query: ' . mysql_error());}
  $row = mysql_fetch_array($result, MYSQL_ASSOC);
  $schedule_id = $row["ID"];
  $start = strtotime( $row["START"] );
  $end = strtotime( $row["END"] );
  
  $start_date = new DateTimeDutch("@".$start, $timezone);
  $start_date->setTimezone($timezone); 

  $reservation_date = new DateTimeDutch("@".$start, $timezone);
  $reservation_date->setTimezone($timezone); 
  
  $end_date = new DateTimeDutch("@".$end, $timezone);
  $end_date->setTimezone($timezone); 

  $sql = "";
  $sql .= "SELECT ID, SIGN_IN_EMAIL, CAST(CONFIRMED AS unsigned int) CONFIRMED ";
  $sql .= "FROM RESERVATIONS ";
  $sql .= "WHERE SCHEDULE_ID = '$schedule_id' ";
  $sql .= "AND UPPER(SIGN_IN_EMAIL) = UPPER('".mysql_escape($email)."') ";
  //echo $sql;
  
  $result = mysql_query($sql);
  if (!$result) {die('Could not exec query: ' . mysql_error());}
  $row = mysql_fetch_array($result, MYSQL_ASSOC);
  $sign_in_has_reservation_today = (isset($row["ID"]) && $row["ID"] != null) ? true : false;
  
  $sql = "";
  $sql .= "SELECT ID, SIGN_IN_EMAIL, CAST(CONFIRMED AS unsigned int) CONFIRMED ";
  $sql .= "FROM RESERVATIONS ";
  $sql .= "WHERE SCHEDULE_ID = '$schedule_id' ";
  //echo $sql;
  
  $result = mysql_query($sql);
  if (!$result) {die('Could not exec query: ' . mysql_error());}
  $row = mysql_fetch_array($result, MYSQL_ASSOC);
  $has_reservation = (isset($row["ID"]) && $row["ID"] != null) ? true : false;
  close();
  
}

function scheduleTable($maxTime) {
  global $start_date;
  global $reservation_date;
  global $end_date;
  global $sql;
  global $result;
  global $has_reservation;
  global $reserved_by_sign_in;
  global $reservation_confirmed;
  global $prev_date_is_reserved;
  global $next_date_is_reserved;
  global $reserved;
  global $sign_in_has_reservation_today;
  global $reservation_id;
  
  if ($reservation_date->format('Y-m-d H:i') < $end_date->format('Y-m-d H:i') && $reservation_date->format('H:i') < $maxTime) {
  ?>
  <table class="ui-widget ui-widget-content ui-corner-all schedule_table">
    <thead>
      <tr>
        <th>Tijd</th>
        <th colspan="2">Duur</th>
      </tr>
    </thead>
    <tbody>
    <?php  

    $i = 0;
    while ($reservation_date->format('Y-m-d H:i') < $end_date->format('Y-m-d H:i') && $reservation_date->format('H:i') < $maxTime) {
      checkReservationsTable();  
      
      $styleClass = "";
      if ($reserved) {
          $styleClass = "RESERVED";
      } else if (($has_reservation == false) || ($prev_date_is_reserved) || ($next_date_is_reserved)) {
        $styleClass = "AVAILABLE";
      } else {
        $styleClass = "NOT_AVAILABLE";
      }
      ?>
      
      <tr id="<?php echo $reservation_date->format('Y-m-d-H-i');?>" class="<?php echo $styleClass; ?>">
        <td class="first">          
          <?php 
          if ((!$reserved) && (!$sign_in_has_reservation_today) && (($has_reservation == false) || ($prev_date_is_reserved) || ($next_date_is_reserved))) {
            // slot is beschikbaar
            ?><a class="time-picker" href="#" onclick="pickTimeSlot('<?php echo $reservation_date->format('Y-m-d');?>', '<?php echo $reservation_date->format('H:i');?>', 20);"><?php echo $reservation_date->format('H:i') ?></a><?php
          } else {
            echo $reservation_date->format('H:i');
          }
          ?>
        </td>
        <td class="second">
          20 min.
        </td>
        <td class="third">
          <div style="position: relative">
          <?php 
          
            if ($reserved_by_sign_in) {
              if ($reservation_confirmed == false) {
                // slot is gereserveerd door jou
                ?>
                Je reservering<br />
                <span class="rm">
                  <a href="#" title="Verwijder reservering" onclick="cancelTimeSlot('<?php echo $reservation_date->format('Y-m-d');?>', '<?php echo $reservation_date->format('H:i');?>', 20, <?php echo $reservation_id;?>);">(verwijder)</a>
                </span>
                
                <?php
              } else {
                ?>Jouw reservering<?php
              }
            } else if ($reserved) {
              // slot is geresveerd door iemand anders
              ?>Gereserveerd<?php
              
            } else if ($sign_in_has_reservation_today) {
              // slot is beschikbaar voor iemand anders
              ?>&nbsp;<?php

            } else if (($has_reservation == false) || ($prev_date_is_reserved) || ($next_date_is_reserved))  {    
              // slot is beschikbaar
              ?><a class="time-picker" href="#" onclick="pickTimeSlot('<?php echo $reservation_date->format('Y-m-d');?>', '<?php echo $reservation_date->format('H:i');?>', 20);">Plan deze tijd!</a><?php

            } else {    
              // kies een naast gelegen slot
              ?>&nbsp;<?php
              
            }       
            
          ?>
          </div>
        </td>
      </tr>
      <?php
        
      //$reservation_date->add(new DateInterval('PT20M'));
      $reservation_date = addMinutes($reservation_date, 20);
      
      $i = $i + 1;             
      
      if ($i > 100) {
        break;
      }
    }
    ?>
    </tbody>
  </table>
  <?php
  }
}  

?>

  <div class="schedule_table_right"> 

    <div class="screen_text" style="margin-left: 10px">
      <div class="blok" >
        Selecteer een <span class="bg">beschikbare tijd</span>
      </div>        
    </div>        

    <table cellpadding="0" cellspacing="0" border="0">
      <tr>
        <td valign="top">
          <?php 
            scheduleTable("13:00");
          ?>
        </td>
        <td valign="top">
          <?php 
            scheduleTable("23:00");
          ?>        
        </td>
      </tr>
    </table>

  </div>
  
